<?php
class lib_category extends spModel
{
	var $pk = "id"; // 每个留言唯一的标志，可以称为主键
	var $table = "class"; // 数据表的名称              
	// 递归查询栏目分类列表
	public function getCatalogList($id = 0, $level = 0)
	{
		$conditions = array('parent' => $id);
		$results = $this->findAll($conditions);
		$catlist = array();
		for($i = 0;$i < $level - 1;$i++)
		{
			$str1 .= "　";
		} 
		if ($level++)
		{
			$str = $str1 . "├ ";
		} ;
		foreach ($results as $cat)
		{
			$id = $cat['id'];
			$name = $str . $cat['name'];
			$dispname = $cat['name'];
			$leftpad = $str1;
			$seq = $cat['sort'];
			$parent = $cat['parent'];
			$status = $cat['status'];
			$value = array('id' => $id, 'name' => $name, 'sort' => $seq, 'parent' => $parent, 'status' => $status, 'leftpad' => $leftpad, 'dispname' => $dispname);
			$catlist[] = $value;
			$catlist = array_merge($catlist, $this->getCatalogList($id, $level));
		} 
		return $catlist;
	} 

	function get2($results, $id, &$sub, &$incate)
	{
		foreach($results as $key => $val)
		{
			if ($id == $val['parent'])
			{
				$sub .= $val['id'] . ',';
				$incate .= $val['name'] . ',';
				$this->get2($results, $val['id'], &$sub, &$incate);
			} 
		} 
		return '';
	} 

	function getCategory($f = 0)
	{
		if ($f <> 0) spAccess('c', 'categorycache') ;
		if ($catecache = spAccess('r', 'categorycache')) return $catecache;
		$results = $this->getCatalogList();
		foreach($results as $key => $val)
		{
			$id = $val['id'] ;
			$sub = $id . ',';
			$incate = $val['name'];
			$this->get2($results, $id, &$sub, &$incate);
			$results[$key]['sub'] = substr($sub, 0, strlen($sub)-1);
			$results[$key]['incate'] = substr($incate, 0, strlen($incate)-1);
		} 
		spAccess('w', 'categorycache', $results, 3600 * 24 * 7) ;
		return $results;
	} 

	function updateCategory()
	{
		$this->getCategory(1);
	} 
	function indexOfID($id)
	{
		$cate = $this->getCategory(); 
		// dump($cate);
		foreach($cate as $val)
		{
			if ($id == $val['id']) return $val['sub'];
		} 
		return '';
	} 

// $s为搜素词
	function getContents($parent = false,$s=false)
	{
		if ($parent)
		{
			if ($subcate = $this->indexOfID($parent))
			{
				$sql = "select a.*,b.*,c.name class,d.screenname from zl_contents a left join (select comment_post_ID,count(comment_post_ID) comments  from  zl_comments  group by comment_post_ID) b on a.id=b.comment_post_id,zl_class c ,zl_user d where d.name=a.author and   a.type='post' and c.id=a.parent   and a.parent  in($subcate)  order by a.created desc limit 0,30" ;
				return $this->findSql($sql);
			} 
			return false;
		} 
		else
		{
		   if ($s) {
			$sql = "select a.*,b.*,c.name class,d.screenname from zl_contents a left join (select comment_post_ID,count(comment_post_ID) comments  from  zl_comments  group by comment_post_ID) b on a.id=b.comment_post_id,zl_class c,zl_user d where  d.name=a.author and   a.type='post' and c.id=a.parent  and  a.title like '%".$s."%'  order by a.created desc limit 0,30" ;
			return $this->findSql($sql);
           
           }
            else {
			$sql = "select a.*,b.*,c.name class,d.screenname from zl_contents a left join (select comment_post_ID,count(comment_post_ID) comments  from  zl_comments  group by comment_post_ID) b on a.id=b.comment_post_id,zl_class c,zl_user d where  d.name=a.author and  a.type='post' and c.id=a.parent   order by   allowCommend  desc ,a.created desc limit 0,30" ;
			return $this->findSql($sql);
		   }
        } 
	} 
	function getTopComments($num = 10)
	{
		$sql = "select a.*,b.title,b.id ,c.screenname from zl_comments  a left join  zl_contents b on a.comment_post_ID=b.id ,zl_user c where c.name=a.comment_author  order by a.comment_ID desc limit 0,$num";
		return $this->findSql($sql);
	} 

	function getCommentList($comment_id = 0, $level = 0, $postid)
	{
		$sql = "select a.*,b.screenname from zl_comments    a,zl_user b  where  a.comment_author=b.name and a.comment_parent=$comment_id and a.comment_post_ID=$postid  order by a.comment_date desc" ;
		$rows = $this->findSql($sql);
		$results = array();     $level+=1;
		foreach($rows as $val)
		{
			$val['level']=$level-1 ;  
            $results[] = $val;
			$results = array_merge($results, $this->getCommentList($val['comment_ID'], $level, $postid));
		} 
		return $results;
	} 

	function getComments($postid = false)
	{
		if ($postid)
		{
			return $this->getCommentList(0, 0, $postid);
		} 
		else
			return false;
	} 
	function getArchive ()
	{
		$sql = "SELECT date_format(created,'%Y-%m') created,count(*) postcount FROM `zl_contents` WHERE date_format(now(),'%Y')= date_format(created,'%Y')  and type='post' group by date_format(created,'%Y-%m')  order by created desc " ;
		$row = $this->findSql($sql);
		$sql = "SELECT date_format(created,'%Y-%m') created,count(*) postcount FROM `zl_contents` WHERE date_format(now(),'%Y')< date_format(created,'%Y')  and type='post' group by date_format(created,'%Y-%m')  order by created desc " ;
		return array_merge ($row, $this->findSql($sql));
	} 

  	function getArchives($d=false)
	{
			$sql = "select a.*,b.*,c.name class  ,d.screenname  from zl_contents a left join (select comment_post_ID,count(comment_post_ID) comments  from  zl_comments  group by comment_post_ID) b on a.id=b.comment_post_id,zl_class c ,zl_user d where   d.name=a.author and  a.type='post'  and c.id=a.parent  and date_format(a.created,'%Y-%m')='".$d."'   order by a.created desc " ;
				return $this->findSql($sql);
	} 


	function category()
	{
		return $this->findAll();
	} 

    function getPublic($id){
      $row=$this->findSql('select public from zl_coments where id='.$id);

    }
} 
